package frog.lazy.framework.keeper.reaml;

import cn.hutool.crypto.SecureUtil;
import frog.lazy.framework.keeper.dto.AccountDTO;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 权限验证
 */
public class JwtRealm extends AuthorizingRealm {

    @Override
    public Class<?> getAuthenticationTokenClass() {
        return AccountDTO.class;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
        AccountDTO accountDTO = (AccountDTO)authenticationToken;
        accountDTO.setPassword(SecureUtil.md5(accountDTO.getPassword()));
        return new SimpleAuthenticationInfo(accountDTO, accountDTO.getPassword(), getName());
    }
}
